package com.xbongbong.paas.service.log.crm.operation.impl;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.log.MongoLogHelp;
import com.xbongbong.paas.log.constant.LogMemoConstant;
import com.xbongbong.paas.log.pojo.InfoArrPojo;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.service.log.crm.operation.AbstractCrmOperationLogHandle;
import com.xbongbong.paas.service.log.crm.operation.CompetitorOperationLogHandle;
import com.xbongbong.pro.competitor.pojo.dto.log.CompetitorPortionVisibleLogDTO;
import com.xbongbong.pro.competitor.pojo.dto.log.CompetitorRemoveVisibleLogDTO;
import com.xbongbong.saas.domain.entity.competitor.CompetitorEntity;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CompetitorEnum;
import com.xbongbong.saas.model.CompetitorModel;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.sys.domain.entity.DepartmentEntity;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.DepartmentModel;
import com.xbongbong.sys.model.UserModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author gaojie
 * @version v1.0
 * @date 2022/8/29 09:22
 * @description
 */
@Slf4j
@Service("competitorOperationLogHandleImpl")
public class CompetitorOperationLogHandleImpl extends AbstractCrmOperationLogHandle implements CompetitorOperationLogHandle {

    @Resource
    private UserModel userModel;
    @Resource
    private CompetitorModel competitorModel;
    @Resource
    private DepartmentModel departmentModel;
    @Resource
    private MongoLogHelp mongoLogHelp;
    @Resource
    private PaasFormModel paasFormModel;

    @Override
    public List<Integer> type() {
        return Arrays.asList(XbbRefTypeEnum.COMPETITOR.getCode());
    }

    @Override
    @Async("sysLogThreadPool")
    public void portionCompetitorUser(CompetitorPortionVisibleLogDTO competitorPortionVisibleLogDTO) {
        try {
            String corpid = competitorPortionVisibleLogDTO.getCorpid();
            String userName = competitorPortionVisibleLogDTO.getLoginUserName();
            String userId = competitorPortionVisibleLogDTO.getUserId();
            List<Long> dataIdList = competitorPortionVisibleLogDTO.getDataIdList();
            List<String> relationIdList = competitorPortionVisibleLogDTO.getRelationIdList();
            List<String> competitorNameList = getCompetitorNameList(dataIdList, corpid);
            List<String> userNameList = getUserNameList(relationIdList, corpid);
            PaasFormEntityExt competitorForm = paasFormModel.getByBusinessType(XbbRefTypeEnum.COMPETITOR.getCode(), corpid);
            String competitorFormName = competitorForm.getName();

            String competitorNameListStr = competitorNameList.stream().collect(Collectors.joining(","));
            String userNameListStr = userNameList.stream().collect(Collectors.joining(","));

            String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_BATCH_PORTION_VISIBLE_USER), userName, competitorFormName, userNameListStr, competitorNameList.size());
            InfoArrPojo infoArrPojo = new InfoArrPojo();
            infoArrPojo.setTitle(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_OPERATION_OBJECT));
            infoArrPojo.setContent(competitorNameListStr);
            mongoLogHelp.buildLog4InfoArr(Collections.singletonList(infoArrPojo),corpid,userId,userName, OperateModuleTypeEnum.COMPETITOR, OperateTypeEnum.SET_VISIBLE_RANGE,"","",
                    memo,competitorPortionVisibleLogDTO.getHttpHeader());

        }catch (Exception e){
            log.error("CompetitorOperationLogHandleImpl#portionCompetitorUser exception:{}",e);
        }
    }

    @Override
    @Async("sysLogThreadPool")
    public void portionCompetitorDep(CompetitorPortionVisibleLogDTO competitorPortionVisibleLogDTO) {
        try {
            String corpid = competitorPortionVisibleLogDTO.getCorpid();
            String userName = competitorPortionVisibleLogDTO.getLoginUserName();
            String userId = competitorPortionVisibleLogDTO.getUserId();
            List<Long> dataIdList = competitorPortionVisibleLogDTO.getDataIdList();
            List<String> relationIdList = competitorPortionVisibleLogDTO.getRelationIdList();
            List<String> competitorNameList = getCompetitorNameList(dataIdList, corpid);
            List<String> depNameList = getDeprNameList(relationIdList, corpid);
            PaasFormEntityExt competitorForm = paasFormModel.getByBusinessType(XbbRefTypeEnum.COMPETITOR.getCode(), corpid);
            String competitorFormName = competitorForm.getName();

            String competitorNameListStr = competitorNameList.stream().collect(Collectors.joining(","));
            String depNameListStr = depNameList.stream().collect(Collectors.joining(","));

            String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_BATCH_PORTION_VISIBLE_DEP), userName, competitorFormName, depNameListStr, competitorNameList.size());
            InfoArrPojo infoArrPojo = new InfoArrPojo();
            infoArrPojo.setTitle(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_OPERATION_OBJECT));
            infoArrPojo.setContent(competitorNameListStr);
            mongoLogHelp.buildLog4InfoArr(Collections.singletonList(infoArrPojo),corpid,userId,userName, OperateModuleTypeEnum.COMPETITOR, OperateTypeEnum.SET_VISIBLE_RANGE,"","",
                    memo,competitorPortionVisibleLogDTO.getHttpHeader());
        }catch (Exception e){
            log.error("CompetitorOperationLogHandleImpl#portionCompetitorUser exception:{}",e);
        }
    }

    @Override
    @Async("sysLogThreadPool")
    public void removeCompetitorUser(CompetitorRemoveVisibleLogDTO competitorRemoveVisibleLogDTO) {
        try {
            String corpid = competitorRemoveVisibleLogDTO.getCorpid();
            String userName = competitorRemoveVisibleLogDTO.getLoginUserName();
            String userId = competitorRemoveVisibleLogDTO.getUserId();
            List<Long> dataIdList = competitorRemoveVisibleLogDTO.getDataIdList();
            List<String> relationIdList = competitorRemoveVisibleLogDTO.getRelationIdList();
            List<String> competitorNameList = getCompetitorNameList(dataIdList, corpid);
            List<String> userNameList = getUserNameList(relationIdList, corpid);
            PaasFormEntityExt competitorForm = paasFormModel.getByBusinessType(XbbRefTypeEnum.COMPETITOR.getCode(), corpid);
            String competitorFormName = competitorForm.getName();

            String competitorNameListStr = competitorNameList.stream().collect(Collectors.joining(","));
            String userNameListStr = userNameList.stream().collect(Collectors.joining(","));

            String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_BATCH_REMOVE_VISIBLE_USER), userName, competitorFormName, userNameListStr, competitorNameList.size());
            InfoArrPojo infoArrPojo = new InfoArrPojo();
            infoArrPojo.setTitle(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_OPERATION_OBJECT));
            infoArrPojo.setContent(competitorNameListStr);
            mongoLogHelp.buildLog4InfoArr(Collections.singletonList(infoArrPojo),corpid,userId,userName, OperateModuleTypeEnum.COMPETITOR, OperateTypeEnum.SET_VISIBLE_RANGE,"","",
                    memo,competitorRemoveVisibleLogDTO.getHttpHeader());

        }catch (Exception e){
            log.error("CompetitorOperationLogHandleImpl#removeCompetitorUser exception:{}",e);
        }
    }

    @Override
    @Async("sysLogThreadPool")
    public void removeCompetitorDep(CompetitorRemoveVisibleLogDTO competitorRemoveVisibleLogDTO) {
        try {
            String corpid = competitorRemoveVisibleLogDTO.getCorpid();
            String userName = competitorRemoveVisibleLogDTO.getLoginUserName();
            String userId = competitorRemoveVisibleLogDTO.getUserId();
            List<Long> dataIdList = competitorRemoveVisibleLogDTO.getDataIdList();
            List<String> relationIdList = competitorRemoveVisibleLogDTO.getRelationIdList();
            List<String> competitorNameList = getCompetitorNameList(dataIdList, corpid);
            List<String> depNameList = getDeprNameList(relationIdList, corpid);
            PaasFormEntityExt competitorForm = paasFormModel.getByBusinessType(XbbRefTypeEnum.COMPETITOR.getCode(), corpid);
            String competitorFormName = competitorForm.getName();

            String competitorNameListStr = competitorNameList.stream().collect(Collectors.joining(","));
            String depNameListStr = depNameList.stream().collect(Collectors.joining(","));
            String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_BATCH_REMOVE_VISIBLE_DEP), userName, competitorFormName, depNameListStr, competitorNameList.size());
            InfoArrPojo infoArrPojo = new InfoArrPojo();
            infoArrPojo.setTitle(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_OPERATION_OBJECT));
            infoArrPojo.setContent(competitorNameListStr);
            mongoLogHelp.buildLog4InfoArr(Collections.singletonList(infoArrPojo),corpid,userId,userName, OperateModuleTypeEnum.COMPETITOR, OperateTypeEnum.SET_VISIBLE_RANGE,"","",
                    memo,competitorRemoveVisibleLogDTO.getHttpHeader());
        }catch (Exception e){
            log.error("CompetitorOperationLogHandleImpl#removeCompetitorDep exception:{}",e);
        }
    }

    /**
     * 获取用户名称列表
     * @param userIdList
     * @param corpid
     * @return
     */
    private List<String> getUserNameList(List<String> userIdList,String corpid){

        Map<String, Object> params = new HashMap<>();
        params.put("userIdIn",userIdList);
        params.put("corpid",corpid);
        List<UserEntity> userEntityList = userModel.findEntitys(params);
        List<String> userNameList = userEntityList.stream().map(user -> user.getName()).collect(Collectors.toList());
        return userNameList;
    }

    /**
     * 获取部门名称列表
     * @param depIdList
     * @param corpid
     * @return
     */
    private List<String> getDeprNameList(List<String> depIdList,String corpid){
        Map<String, Object> params = new HashMap<>();
        params.put("idIn",depIdList);
        params.put("corpid",corpid);
        List<DepartmentEntity> departmentEntityList = departmentModel.findEntitys(params);
        List<String> depNameList = departmentEntityList.stream().map(item -> item.getName()).collect(Collectors.toList());
        return depNameList;
    }


    /**
     * 获取竞争对手名称列表
     * @param competitorIdList
     * @param corpid
     * @return
     */
    private List<String> getCompetitorNameList(List<Long> competitorIdList,String corpid){
        Map<String, Object> params = new HashMap<>();
        params.put("ids",competitorIdList);
        params.put("corpid",corpid);
        List<CompetitorEntity> competitorEntityList = competitorModel.findEntitys(params);
        List<String> competitorNameList = competitorEntityList.stream().map(item -> FastJsonHelper.getStringOrDefault(item.getData(), CompetitorEnum.NAME.getAttr(), "")).collect(Collectors.toList());
        return competitorNameList;
    }


}
